#include "../head/server.h"

int ls_work(int net_fd, char *username, MYSQL *mysql)
{
	char sql[1024] = {0};
	char all_filename[1024] = {0};
	sprintf(sql, "select filename from all_user_file where parent_id = (select id from all_user_file where file_path = (select current_path from user_info where username = '%s') and username = '%s');", username, username);
	puts(sql);
	int qret = mysql_query(mysql, sql);
	if (qret != 0)
	{
		fprintf(stderr, "mysql_query:%s\n", mysql_error(mysql));
	}
	MYSQL_RES *res = mysql_store_result(mysql);
	if(res == NULL) {
		mysql_free_result(res);
		train_t train;
		bzero(&train, sizeof(train));
		train.length = 0;
		send(net_fd, &train, sizeof(train.length) + train.length, MSG_WAITALL);
		return 0;
	}
	MYSQL_ROW row;
	while((row = mysql_fetch_row(res)) != NULL) {
		if(row[0] != NULL) {
			strcat(all_filename, row[0]);
			strcat(all_filename, "/");
		}
	}
	mysql_free_result(res);

	train_t train;
	bzero(&train, sizeof(train));
	train.length = strlen(all_filename) - 1;
	if(train.length < 0) {
		train.length = 0;
	}
	memcpy(train.data, all_filename, train.length);
	send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
	return 0;
}